<?php

namespace app\api\controller\fruit\tt\v1;

use app\admin\model\TtFruitUser;
use think\cache\driver\Redis;

class Warriorother extends Warrior
{

    // 幸运转盘数据
    public function lucky_wheel()
    {
        $game_const = $this->get_game_const();
        $award_list = explode(',', $game_const->lucky_wheel->award);
        // 拿5个普通奖励
        $index_list = array_rand($award_list, 5);
        $list = array();
        for ($i = 0; $i < 5; $i++) {
            $list[$i + 1] = $award_list[$index_list[$i]];
        }
        // 拿取一个该用户没有得皮肤奖励
        $list[6] = $this->get_weapon_award_str();
        if (!$list[6]) {
            $list[6] = $award_list[array_rand($award_list, 1)];
        }
        $this->result('ok', $list);
    }

    // 转动转盘
    public function do_lucky_wheel()
    {
        $award_list = json_decode($this->request->post('award_list'), true);
        // 随机抽取
        $award_index = mt_rand(1, 5);
        $award = $award_list[$award_index];
        $this->handle_award($award);
        if ($this->user->save() !== false) {
            // 处理转盘成就
            $this->handle_achieve('lucky_wheel_num');
            $this->result('ok', $award_index);
        }
        $this->error('fail');
    }

    // 获取签到数据
    public function sign_in_info()
    {
        // 今天0点
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        if ($this->user->last_sign_in < $beginToday) {
            $this->user->is_sign_in = 0;
            $this->user->save();
        }
        // 签到轮数
        $data['sign_in_round'] = floor($this->user->sign_in_num / 7) == 0 ? 1 : 2;
        // 下一次签到天数
        $data['sign_in_num'] = floor($this->user->sign_in_num % 7) + 1;
        // 今日是否签过到
        $data['is_sign_in'] = $this->user->is_sign_in;

        // 如果签到过就减1
        if ($this->user->is_sign_in == 1) {
            $data['sign_in_num'] -= 1;
            // 第一轮的最后一天处理
            if ($this->user->sign_in_num == 7) {
                $data['sign_in_round'] = 1;
                $data['sign_in_num'] = 7;
            }

        }
        $this->result('sign in', $data);
    }

    // 签到领取奖励
    public function sign_in()
    {
        $multiple = $this->request->param('multiple') ?: 1;
        // 读取签到json
        $sign_condition = $this->get_sign_condition();
        $sign_round = floor($this->user->sign_in_num / 7) + 1;
        $sign_in_num = floor($this->user->sign_in_num % 7) + 1;
        $sign = $sign_condition->$sign_in_num;
        // 对比轮数
        if ($sign_round == 1) {
            $award = $sign->award1;
        } else {
            $award = $sign->award2;
        }
        $award_str = $this->handle_award($award, $multiple);
        $this->user->sign_in_num += 1;
        $this->user->is_sign_in = 1;
        $this->user->last_sign_in = time();
        if ($this->user->save() !== false) {
            $data['award'] = $award_str;
            $this->result('ok', $data);
        }
        $this->error('fail');
    }

    // 抽卡奖励
    public function lucky_card()
    {
        // 拿到奖励池数据
        $game_const = $this->get_game_const();
        $coin_award_list = explode(',', $game_const->lucky_card_1->award);
        $diamond_award_list = explode(',', $game_const->lucky_card_2->award);

        // 拼接奖励(2.0.2版本调整)
        if ($this->compare_version($this->version, '2.0.2') >= 0) {
            // 第一张  金币
            $award1 = $coin_award_list[array_rand($coin_award_list, 1)];
            // 第二张  钻石
            $award2 = $diamond_award_list[array_rand($diamond_award_list, 1)];
            // 第三张  金币+钻石
            $award3 = $award1 . ',' . $award2;
        } else {
            // 第一张  金币
            $award1 = $coin_award_list[array_rand($coin_award_list, 1)];
            // 第二张  金币+钻石
            $award2 = $coin_award_list[array_rand($coin_award_list, 1)] . ',' . $diamond_award_list[array_rand($diamond_award_list, 1)];
            // 第三张  金币+钻石+屠龙宝刀
            $award3 = $coin_award_list[array_rand($coin_award_list, 1)] . ',' . $diamond_award_list[array_rand($diamond_award_list, 1)] . ',' . 'a_3_2';
        }

        $data = [$award1, $award2, $award3];
        shuffle($data);
        $this->result('result', $data);
    }

    // 抽卡开奖
    public function do_lucky_card()
    {
        $awards = $this->request->post('awards');
        // 遍历获取奖励
        $awards = explode(',', $awards);
        foreach ($awards as $award) {
            $this->handle_award($award);
        }
        if ($this->user->save() !== false) {
            $this->result('ok');
        }
        $this->error('fail');
    }

    // 武器奖励字符串(玩家未拥有的)
    public function get_weapon_award_str()
    {
        $weapon_list = $this->get_weapon_define();
        $have_weapon_list = explode(',', $this->user->weapon_list);
        // 遍历取出玩家为拥有的
        $not_have_weapon_list = array();
        foreach ($weapon_list as $weapon) {
            if ($weapon && $weapon->type == 1) {
                if (!in_array($weapon->id, $have_weapon_list)) {
                    array_push($not_have_weapon_list, $weapon->id);
                }
            }
        }
        if (count($not_have_weapon_list) !== 0) {
            $item_id = $this->get_item_by_weapon($not_have_weapon_list[array_rand($not_have_weapon_list, 1)]);
            return 'a_' . $item_id . '_1';
        } else {
            return false;
        }
    }

    // 查询午晚餐福利数据
    public function query_welfare()
    {
        $redis = new Redis();
        $hours = intval(date('H', time())); // 当前小时数
        $game_const = $this->get_game_const();
        // 午餐福利
        $data['lunch'] = array(
            'can_get' => $hours >= 12 && $hours < 14 ? true : false,
            'award' => $game_const->lunch_award->award,
            'is_get' => $redis->get($this->user->openid . '_tt_fruit_lunch') ? true : false,
        );
        // 晚餐福利
        $data['dinner'] = array(
            'can_get' => $hours >= 16 && $hours < 21 ? true : false,
            'award' => $game_const->dinner_award->award,
            'is_get' => $redis->get($this->user->openid . '_tt_fruit_dinner') ? true : false,
        );
        $this->result('welfare', $data);
    }

    // 获取邀请好友数据
    public function get_invite_info()
    {
        $game_const = $this->get_game_const();
        $invite_list = explode(',', $this->user->invite_info);
        $data['invite_list'] = [];
        foreach ($invite_list as $item) {
            $invite = TtFruitUser::get($item);
            if ($invite) {
                $data['invite_list'][] = ['nick_name' => $invite->nick_name, 'avatar_url' => $invite->avatar_url];
            }
        }
        $data['num'] = count($data['invite_list']);
        $data['award'] = $game_const->invite_award->award;
        $data['is_get'] = $this->user->invite_award ? true : false;
        $this->result('ok', $data);
    }

    // 领取福利
    public function get_welfare()
    {
        $type = $this->request->post('type'); // 福利类型
        $game_const = $this->get_game_const();
        // 对类型分类判断是否能领取,并进行加奖励操作
        switch ($type) {
            case "lunch_award" :
                $redis = new Redis();
                $end_day_time = strtotime(date('Y-m-d', strtotime('+1 day'))) - time(); // 今日结束剩余时间
                $hours = intval(date('H', time())); // 当前小时数
                if ($hours >= 12 && $hours < 14) {
                    $is_get = $redis->get($this->user->openid . '_tt_fruit_lunch');
                    if (!$is_get) {
                        $awards_str = $game_const->lunch_award->award;
                        $awards = explode(',', $awards_str);
                        foreach ($awards as $award) {
                            $this->handle_award($award);
                        }
                        if ($this->user->save() !== false) {
                            $redis->set($this->user->openid . '_tt_fruit_lunch', 1, $end_day_time);
                            $this->result('ok', $awards_str);
                        }
                    }
                }
                break;
            case "dinner_award" :
                $redis = new Redis();
                $end_day_time = strtotime(date('Y-m-d', strtotime('+1 day'))) - time(); // 今日结束剩余时间
                $hours = intval(date('H', time())); // 当前小时数
                if ($hours >= 16 && $hours < 21) {
                    $is_get = $redis->get($this->user->openid . '_tt_fruit_dinner');
                    if (!$is_get) {
                        $awards_str = $game_const->dinner_award->award;
                        $awards = explode(',', $awards_str);
                        foreach ($awards as $award) {
                            $this->handle_award($award);
                        }
                        if ($this->user->save() !== false) {
                            $redis->set($this->user->openid . '_tt_fruit_dinner', 1, $end_day_time);
                            $this->result('ok', $awards_str);
                        }
                    }
                }
                break;
            case "invite_award" :
                $invite_list = explode(',', $this->user->invite_info);
                if (!$this->user->invite_award && count($invite_list) == 5) {
                    $awards_str = $game_const->invite_award->award;
                    $awards = explode(',', $awards_str);
                    foreach ($awards as $award) {
                        $this->handle_award($award);
                    }
                    $this->user->invite_award = 1;
                    if ($this->user->save() !== false) {
                        $this->result('ok', $awards_str);
                    }
                }
                break;
            case "enter_xcx" :
                if (!$this->user->enter_xcx_award) {
                    $awards_str = $game_const->enter_xcx->award;
                    $awards = explode(',', $awards_str);
                    foreach ($awards as $award) {
                        $this->handle_award($award);
                    }
                    $this->user->enter_xcx_award = 1;
                    if ($this->user->save() !== false) {
                        $this->result('ok', $awards_str);
                    }
                }
                break;
            case "share_award" :
                $awards_str = $game_const->share_award->award;
                $awards = explode(',', $awards_str);
                foreach ($awards as $award) {
                    $this->handle_award($award);
                }
                if ($this->user->save() !== false) {
                    $this->result('ok', $awards_str);
                }
                break;
        }
        $this->error('error');
    }

    // 获取分享信息
    public function get_share_info()
    {
        $type = $this->request->param('type');

        $this->handle_achieve('share_num');

        // 分享类型
        if ($type == 'show') {
            $res = array(
                'title' => '',
                'imgUrl' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/fruit_warrior/show/1.png',
            );
        } else {
            $data = array(
                array(
                    'title' => '惊！水果还能这么切',
                    'imgUrl' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/fruit_warrior/share/2.jpg'
                ),
                array(
                    'title' => '这么多年你不知道的切水果技巧',
                    'imgUrl' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/fruit_warrior/share/6.jpg'
                )
            );
            // 随机取出一个分享信息
            $res = $data[array_rand($data, 1)];
        }
        $this->result('ok', $res);
    }

    // 红点
    public function red_point()
    {
        $type_list = $this->request->post('type_list');
        $type_list = explode(',', $type_list);

        if ($type_list) {
            // 对各种红点展示进行判断
            $data = [];
            foreach ($type_list as $type) {
                switch ($type) {
                    case 'achieve':
                        $data['achieve'] = $this->achieve();
                        break;
                    case 'lunch': break;
                    case 'dinner': break;
                    default: break;
                }
            }
            $this->result('ok', $data);
        }
        $this->result('fail');
    }

}
